package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Add;
import com.ruoyi.common.annotation.Edit;
import com.ruoyi.system.domain.dto.CommunityBuildingDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CommunityBuilding;
import com.ruoyi.system.service.ICommunityBuildingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * buildingController
 * 
 * @author hanpee
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/system/building")
public class CommunityBuildingController extends BaseController
{
    @Autowired
    private ICommunityBuildingService communityBuildingService;

    /**
     * 查询building列表
     */
    @PreAuthorize("@ss.hasPermi('system:building:list')")
    @GetMapping(value = "/list")
    public TableDataInfo list(CommunityBuilding communityBuilding)
    {
        startPage();
        List<CommunityBuilding> list = communityBuildingService.selectCommunityBuildingList(communityBuilding);
        return getDataTable(list);
    }

    /**
     * 导出building列表
     */
    @PreAuthorize("@ss.hasPermi('system:building:export')")
    @Log(title = "building", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityBuilding communityBuilding)
    {
        List<CommunityBuilding> list = communityBuildingService.selectCommunityBuildingList(communityBuilding);
        ExcelUtil<CommunityBuilding> util = new ExcelUtil<CommunityBuilding>(CommunityBuilding.class);
        util.exportExcel(response, list, "building数据");
    }

    /**
     * 获取building详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:building:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(communityBuildingService.selectCommunityBuildingById(id));
    }

    /**
     * 新增building
     */
    @PreAuthorize("@ss.hasPermi('system:building:add')")
    @Log(title = "building", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated(Add.class) @RequestBody CommunityBuilding communityBuilding) throws Exception {
        return toAjax(communityBuildingService.insertCommunityBuilding(communityBuilding));
    }

    /**
     * 修改building
     */
    @PreAuthorize("@ss.hasPermi('system:building:edit')")
    @Log(title = "building", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated(Edit.class) @RequestBody CommunityBuilding communityBuilding) throws Exception {
            try{
                return toAjax(communityBuildingService.updateCommunityBuilding(communityBuilding));
            } catch(Exception e) {
                return AjaxResult.error("当前楼宇已存在");
            }
    }

    /**
     * 批量删除building
     */
    @PreAuthorize("@ss.hasPermi('system:building:remove')")
    @Log(title = "building", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(communityBuildingService.deleteCommunityBuildingByIds(ids));
    }

    /**
     * 单id删除
     * 判断是否有单元
     */
    @DeleteMapping("/del/{id}")
    public AjaxResult remove(@PathVariable Long id) throws Exception {
        return toAjax(communityBuildingService.deleteCommunityBuildingById(id));
    }


    /**
     * echarts查询数据
     */
    @ApiOperation("echarts查询数据")
    @GetMapping("/listRooms")
    public List<CommunityBuildingDto> listRooms(CommunityBuildingDto communityBuildingDto) {
        return communityBuildingService.listRooms(communityBuildingDto);
    }

    /**
     * 查询所有的数据
     * @param communityBuildingDto communityBuildingDto类
     * @return 表格分页数据对象
     */
    @PostMapping("building")
    public TableDataInfo listBuilding(CommunityBuildingDto communityBuildingDto) {
        return getDataTable(communityBuildingService.listBuilding());
    }
}
